//
//  SYAlertViewController.m
//  huisaoche
//
//  Created by Steven.Yung on 2017/7/11.
//  Copyright © 2017年 Minidai. All rights reserved.
//

#import "SYAlertViewController.h"

//默认字体颜色
#define DefaultTextColor [UIColor colorWithRed:181/255.0 green:181/255.0 blue:181/255.0 alpha:1.0]
//其他按钮的默认颜色
#define DefaultOtherButtonColor [UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0]
#define BackGroundColor  [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1.0]
//每一行的高度
#define SectionHeight    44.0
//View的宽度
#define ViewWidth          [UIScreen mainScreen].bounds.size.width
#define SYMainScreenHeight [UIScreen mainScreen].bounds.size.height
//取消按钮与其他按钮之间的间距
#define CancelButtonSpace 5.0

@interface SYAlertViewController ()

@property(nonatomic, copy) NSString *alertTitle;
@property(nonatomic, copy) NSString *cancelTitle;
@property(nonatomic, strong) NSArray *otherTitles;

@property(nonatomic, strong) UIView *containerView;
@property(nonatomic, strong) UIButton *cancelButton;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) NSMutableArray *otherButtons;

@property(nonatomic, copy) CancelHandler _Nullable cancelHandler;
@property(nonatomic, copy) OtherButtonClickHandler _Nonnull otherButtonClickHandler;

@end

@implementation SYAlertViewController

+ (instancetype)alertWithTitle: (NSString *)title
                   cancelTitle: (NSString *)cancelTitle
             otherButtonTitles: (NSArray<NSString *> *)otherTitles
                 cancelHandler: (CancelHandler)cancel
       otherButtonClickHandler: (OtherButtonClickHandler)otherButtonClick {
    
    SYAlertViewController *alert = [[SYAlertViewController alloc] initWithTitle:title cancelTitle:cancelTitle otherTitle:otherTitles];
    alert.cancelHandler = cancel;
    alert.otherButtonClickHandler = otherButtonClick;
    
    return alert;
}

- (void)showInViewController:(UIViewController *)viewController {
    [KeyWindow addSubview:self.view];
    [viewController addChildViewController:self];
}

- (instancetype)initWithTitle:(NSString *)alertTitle cancelTitle:(NSString *)cancelTitle otherTitle: (NSArray *)otherTitles {
    self = [super init];
    
    if (self) {
        self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.alertTitle = alertTitle;
        self.cancelTitle = cancelTitle;
        self.otherTitles = otherTitles;
        [self setupUI];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.preferredContentSize = CGSizeMake(ViewWidth, SYMainScreenHeight - 64);
    __weak __typeof(self)weakSelf = self;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf cancelButtonClick:nil];
    }];
    
    [self.view addGestureRecognizer:tapGesture];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    CGRect frame = self.containerView.frame;
    
    frame.origin.y = self.view.superview.bounds.size.height - frame.size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
       
        [self.containerView setFrame:frame];
    }];
}

#pragma mark - Setup UI

- (void)setupUI {
    
    CGFloat containerViewHeight = SectionHeight + CancelButtonSpace + self.otherTitles.count * SectionHeight;
    if (self.alertTitle) {
        //如果标题不为空，containerView的高度为取消按钮的高度+间隔+标题高度+其他按钮的个数*按钮高度
        containerViewHeight += SectionHeight;
    }

    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, SYMainScreenHeight, ViewWidth, containerViewHeight)];
    self.containerView.backgroundColor = BackGroundColor;
    [self.view addSubview:self.containerView];
    
    self.cancelButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    [self.cancelButton setFrame: CGRectMake(0, containerViewHeight - SectionHeight, ViewWidth, SectionHeight)];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateHighlighted];
    [self.cancelButton setTitleColor:DefaultTextColor forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:DefaultTextColor forState:UIControlStateHighlighted];
//    [self.cancelButton setBackgroundColor:[UIColor whiteColor]];
    [self.cancelButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.cancelButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.98 alpha:1.0]] forState:UIControlStateHighlighted];
    [self.cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.containerView addSubview:self.cancelButton];
    
    //其他按钮的起始Y坐标
    CGFloat otherButtonYPosition = 0;
    
    if (self.alertTitle) {
        otherButtonYPosition = SectionHeight;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, SectionHeight)];
        self.titleLabel.backgroundColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:12.0];
        self.titleLabel.textColor = DefaultTextColor;
        self.titleLabel.text = self.alertTitle;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.containerView addSubview:self.titleLabel];
    }
    
    self.otherButtons = [NSMutableArray array];
    for (NSInteger i = 0; i < self.otherTitles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame: CGRectMake(0, otherButtonYPosition, ViewWidth, SectionHeight)];
        button.layer.borderColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1.0].CGColor;
        button.layer.borderWidth = 1.0;
//        button.qmui_borderPosition = QMUIBorderViewPositionTop;
        button.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [button setTitle:self.otherTitles[i] forState:UIControlStateNormal];
        [button setTitle:self.otherTitles[i] forState:UIControlStateHighlighted];
        //默认字体颜色为系统默认颜色
        [button setTitleColor:DefaultOtherButtonColor forState:UIControlStateNormal];
        [button setTitleColor:DefaultOtherButtonColor forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.98 alpha:1.0]] forState:UIControlStateHighlighted];
        button.tag = i;
        [button addTarget:self action:@selector(otherButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.containerView addSubview:button];
        otherButtonYPosition += SectionHeight;
        
        [self.otherButtons addObject:button];
    }
}

- (void)cancelButtonClick:(UIButton *)sender {
    if (self.cancelHandler) {
        self.cancelHandler();
    }
    [self dismissAlertViewController];
}

- (void)otherButtonClick:(UIButton *)sender {
    
    if (self.otherButtonClickHandler) {
        self.otherButtonClickHandler(sender.tag);
    }
    [self dismissAlertViewController];
}

- (void)dismissAlertViewController {
    [UIView animateWithDuration:0.25
                     animations:^{
                         [self.containerView setFrame:CGRectMake(0, SYMainScreenHeight, ViewWidth, self.containerView.frame.size.height)];
                         self.view.alpha = 0.0;
                     }
                     completion:^(BOOL finished) {
                         [self didMoveToParentViewController:nil];
                         [self.view removeFromSuperview];
                         [self removeFromParentViewController];
                     }];
}

#pragma mark - Setter

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    
    self.titleLabel.textColor = titleColor;
}

- (void)setTitleFontSize:(CGFloat)titleFontSize {
    _titleFontSize = titleFontSize;
    
    if (self.titleLabel) {
        self.titleLabel.font = [UIFont systemFontOfSize:titleFontSize];
    }
}

- (void)setCancelTitleColor:(UIColor *)cancelTitleColor {
    _cancelTitleColor = cancelTitleColor;
    
    [self.cancelButton setTitleColor:cancelTitleColor forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:cancelTitleColor forState:UIControlStateHighlighted];
}

- (void)setCancelTitleFontSize:(CGFloat)cancelTitleFontSize {
    _cancelTitleFontSize = cancelTitleFontSize;
    
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:cancelTitleFontSize];
}

- (void)setOtherTitleColor:(UIColor *)otherTitleColor {
    _otherTitleColor = otherTitleColor;
    
    for (UIButton *button in self.otherButtons) {
        [button setTitleColor:otherTitleColor forState:UIControlStateNormal];
        [button setTitleColor:otherTitleColor forState:UIControlStateHighlighted];
    }
}

- (void)setOtherTitleFontSize:(CGFloat)otherTitleFontSize {
    _otherTitleFontSize = otherTitleFontSize;
    
    for (UIButton *button in self.otherButtons) {
        button.titleLabel.font = [UIFont systemFontOfSize:otherTitleFontSize];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
